package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.ResponseResult;
import com.example.demo.entity.Sales;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.SalesMapper;
import com.example.demo.service.SalesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SalesServiceImpl extends ServiceImpl<SalesMapper, Sales> implements SalesService {

    @Resource
    private GoodsMapper goodsMapper;


    /**
     * 查询全部销售单信息
     * @return
     */
    @Override
    public ResponseResult getList() {
        List<Sales> sales = list();
        if(sales.isEmpty()){
            return ResponseResult.ok("没有销售单信息");
        }
        return ResponseResult.ok(sales);
    }

    @Override
    @Transactional
    public ResponseResult saveSales(Sales sales) {
        /**
         * 进来第一步先判读是否满足条件
         */
        String amount = sales.getAmount();
        String goodsName = sales.getGoodsName();

//        goodsMapper.selectOne();


        boolean isSuccess = save(sales);
        if(!isSuccess){
            return ResponseResult.fail("新增失败");
        }
        /**
         * 这里要进行判断  如果不满足条件  就是 库存里面没有东西或者数量不足  那么就不可以录入成功
         */
        return ResponseResult.ok("新增成功");
    }

    @Override
    public ResponseResult updateSales(Sales sales) {
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Sales_id",sales.getId());
        boolean isSuccess = update(sales, queryWrapper);
        if(!isSuccess){
            return ResponseResult.fail("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    @Override
    public ResponseResult deleteById(String id) {
        boolean isSuccess = remove(new QueryWrapper<Sales>().eq("Sales_id", id));
        if(!isSuccess){
            return ResponseResult.fail("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }
}
